<?php

namespace app\services\data\stat\ad\normal;

class StatFirstUp extends StatBaseReportFirstup
{
    public function getSql()
    {
        $join             = $this->queryJoin('firstups');
        $where            = $this->queryWhere(['time', 'game_id', 'channel', 'game_channel_id', 'media_id', 'media_docking_id', 'plan_id', 'source_type', 'device_type'], 'firstups');
        $plan_where       = $this->queryWhere(['owner', 'plan_group_id'], 'ad_plan');
        $media_user_where = $this->queryWhere(['daili', 'media_user_id'], 'ad_media_user');

        $sql = "
            SELECT count(DISTINCT `#id`) AS firstup, count(DISTINCT `ip`) AS firstup_ip
            FROM firstups FINAL
            {$join}
            WHERE {$where}
            AND {$plan_where}
            AND {$media_user_where}
        ";

        return $sql;
    }

    public function getSqlByGroup()
    {
        $join             = $this->queryJoin('firstups');
        $where            = $this->queryWhere(['time', 'game_id', 'channel', 'game_channel_id', 'media_id', 'media_docking_id', 'plan_id', 'source_type', 'device_type'], 'firstups');
        $plan_where       = $this->queryWhere(['owner', 'plan_group_id'], 'ad_plan');
        $media_user_where = $this->queryWhere(['daili', 'media_user_id'], 'ad_media_user');

        list($select_str, $groupby_str) = $this->queryGroupBy('firstups');

        $sql = "
            SELECT {$select_str}, count(DISTINCT `#id`) AS firstup, count(DISTINCT `ip`) AS firstup_ip
            FROM firstups
            {$join}
            WHERE {$where}
            AND {$plan_where}
            AND {$media_user_where}
            GROUP BY {$groupby_str}
        ";

        return $sql;
    }
}
